# 创建集合
a = {1,3,5,7,9}
a1 = set()
a2 = set(('a','c','b','e','f','g'))
print('a2------>',a2)

# 列表去重
a3 = set([1,2,3,4,6,2,3])
print('a3--------->',a3)
a4 = set(range(8))
print('a4---------->',a4)
a5 = set({'a':11,'b':13,'c':14,'d':15})
print(a5)
a6 = {('a','b'),'asc',1,3,5}
print('a6---------->',a6)


#a7 = {('a','b'),'asc',1,3,5,[1,2,3]} # TypeError: unhashable type: 'list'
#print('a7---------->',a7)

# 集合无序，不支持索引、切片


# 遍历（由于无序所以每次遍历结果都不一样）
for val in a5:
    print(val)

for inx,val in enumerate(a5):
    print(inx,val)

# 成员判断
res = 'a' in a5
print(res)

res1 = 'b' not in a5
print(res1)

# 集合不支持运算符（+，*）操作

# 增
print('a5------>',a5)
res2 = a5.add('g')
print(res2)
print('a5------->',a5)

res3 = a5.add('abc')
print('a5------->',a5)

# 删
print('a5------>',a5)
#res4 = a5.remove('gg') # 删除元素不存在会抛出：KeyError: 'gg'
res4 = a5.discard('gg') # 删除元素不存在不会抛异常
print(res4)
print('a5------->',a5)

print('a5------->',a5)
res5 = a5.pop() # 随机删除集合中的一个元素，并弹出
print(res5)
print('a5------->',a5)

print('a3------------>',a3)
print(a3.clear())
print('a3------------>',a3)

# 改(新增)
print('a5------>',a5)
res6 = a5.update(('1243254','xvbnabc'))
print(res6)
print('a5------->',a5)

print('a5------>',a5)
res7 = a5.update('1243254','xvbnabc') # 可接受多个位置参数且字符串会被拆分
print(res7)
print('a5------->',a5)

# 查（成员判断，看某元素是否存在集合中）


# 交集
b = {1,2,3,5,8,9}
b1 = {1,3,0,11,13,15}
res9 = b & b1
print('res9----->',res9)



# 并集
res8 = b | b1
print('res8----->',res8)

# 差集
res9 = b -b1
print('res9-------->',res9)

res10 = b1 -b
print('res10-------->',res10)

res11 = b.difference(b1)
print('res11--------->',res11)

res12 = b1.difference(b)
print('res12--------->',res12)

# 子集
res13 = res12.issubset(b1)
print('--------->res13',res13)


# 父级
res14 = b1.issubset(res12)
print('--------->res14',res14)

# 有无交集
res15 = b.isdisjoint(b1)
print('------->res15',res15)